package tt.com.warehousemanager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tt.com.common.enums.Status;
import tt.com.common.model.YxCommonWarehouse;
import tt.com.common.util.CommonResult;
import tt.com.common.util.ObjectUtil;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.service.WarehouseService;

import javax.servlet.http.HttpSession;

/**
 * @author Czycka
 * @date 2022/8/26 2:45
 * @description 仓库控制类
 */
@RestController
@RequestMapping(value = "warehouse/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @PostMapping(value = "insert")
    public CommonResult insert(@RequestBody YxCommonWarehouse warehouse, HttpSession session) {
        if (!ObjectUtil.objectIsNull(warehouse)) {
            return CommonResult.buildR(Status.PARAM_ISNULL,"参数为空");
        }
        if (warehouseService.insertWareHouse(warehouse, "2") > 0) {
            return CommonResult.buildR(Status.OK, "仓库添加成功");
        } else {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "仓库添加失败");
        }
    }

    @PostMapping(value = "delete/{id}")
    public CommonResult delete(@PathVariable("id") Long id, HttpSession session) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL,"参数为空");
        }
        System.err.println(id);
        Integer result = warehouseService.deleteWareHouse(id, "2");
        if (result == -1) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "仓库不存在");
        } else if (result == -2) {
            return CommonResult.buildR(Status.SYSTEM_ERROR, "无法重复删除");
        } else {
            return CommonResult.buildR(Status.OK, "仓库删除成功");
        }
    }

    @PostMapping(value = "update")
    public CommonResult update(@RequestBody YxCommonWarehouse warehouse, HttpSession session) {
        if (!ObjectUtil.objectIsNull(warehouse)) {
            return CommonResult.buildR(Status.PARAM_ISNULL,"参数为空");
        }
        if (warehouseService.updateWareHouse(warehouse, "2") > 0) {
            return CommonResult.buildR(Status.OK, "仓库修改成功");
        }else{
            return CommonResult.buildR(Status.SYSTEM_ERROR, "仓库修改失败");
        }
    }

    @PostMapping(value = "show/page")
    public CommonResult showPage(@RequestBody PageUtil pageUtil) {
        if (pageUtil.getPageIndex() == null || pageUtil.getPageSize() == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        pageUtil = warehouseService.showWareHouseByPage(pageUtil);
        return CommonResult.buildR(Status.OK, pageUtil);
    }

    @PostMapping(value = "show/all")
    public CommonResult showAll() {
        return CommonResult.buildR(Status.OK,"查询成功",warehouseService.list());
    }

    @PostMapping(value = "show/{id}")
    public CommonResult show(@PathVariable("id") Long id) {
        if (id == null) {
            return CommonResult.buildR(Status.PARAM_ISNULL, "参数为空");
        }
        return CommonResult.buildR(Status.OK,"查询成功",warehouseService.getById(id));
    }
}
